package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.processing.objects.ObjectTypeId;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.text.SimpleDateFormat;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.MessagesDAO;
import com.edushare.common.vo.MessageComments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class CommentDisplay extends Panel {
	public CommentDisplay(String id, final IModel<MessageComments> model) {
		super(id, model);
		add(new Label("message", model.getObject().getComment())
				.setEscapeModelStrings(false));
		add(new Label("date", new SimpleDateFormat("MMM d, yyyy hh:mm a")
				.format(model.getObject().getCreationTimestamp())));
		Panel p;
		add(p = new EduShareURLPanel("sender", new CayenneModel<Users>(model
				.getObject().getUser())));
		p.add(new SimpleAttributeModifier("style", "font-weight:bold;"));
		add(new ProfileImage("image", new CayenneModel<IFileUploadable>(model
				.getObject().getUser()), ApplicationConstants.FILE_SUFFIX_ICON));

		add(new Link<Void>("delete") {
			private static final long serialVersionUID = 8562869452062856917L;

			@Override
			public boolean isVisible() {
				if (!super.isVisible())
					return false;
				Messages mess = model.getObject().getMessage();
				Users u = EduShareSession.get().getLoggedInUser();
				
				if(mess.getReceiverType()==Messages.RECEIVER_TYPE_USER && 
						mess.getAllMessageRecipients().contains(u))
					return true;
				
				CayenneList<Users> owners = ObjectTypeId.getOwner(mess
						.getReceiverId(), mess.getReceiverType());
				if (owners.contains(u)) {
					return true;
				}
				return false;
			}

			@Override
			public void onClick() {
				new MessagesDAO(EduShareSession.get().getSessionORMInterface())
						.deleteComment(model.getObject());
			}
		});

	}

	private static final long serialVersionUID = -4235984360632763722L;
}
